n,m,t=map(int,input().split())

visit=[[[False for i in range(0,301)] for j in range(0,101)] for k in range(0,101)]

for i in range(0,t):
    x,y,s,e=map(int,input().split())
    visit[x][y][s:e+1]=[True for i in range(s,e+1)]

import queue
q=queue.deque()

result=0
q.append([1,1,1])

turn=[[0,1],[1,0],[-1,0],[0,-1]]

while True:
    now=q.popleft()
    x,y,t=now[:]

    for k in range(0,4):
        i=x+turn[k][0]
        j=y+turn[k][1]
        if i<=0 or j<=0 or i>n or j>m:
            continue
        if visit[i][j][t]:
            continue
        
        visit[i][j][t]=True

        if i==n and j==m:
            print(t)
            exit(0)
        q.append([i,j,t+1])


